package com.igeek.compus.backend.system.interceptor;

import cn.hutool.core.util.ObjectUtil;
import com.igeek.compus.backend.system.Constants;
import com.igeek.compus.backend.sysuser.entity.SysUser;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@Component
public class VerificationHandlerInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {

		SysUser sysUser = (SysUser) request.getSession().getAttribute(Constants.SYS_USER);

		String path = request.getServletPath();

		if(-1 != path.indexOf("/login") || -1 != path.indexOf("/api") || -1 != path.indexOf("/css")
				|| -1 != path.indexOf("/images") || -1 != path.indexOf("/js") || -1 != path.indexOf("/lib")
				|| -1 != path.indexOf("/menu")) {
			//  部分入口不拦截
			return true;
		}else{
			if(ObjectUtil.isNotEmpty(sysUser)) {
				return true;
			}else {
				//  session中user为空则返回至登陆页面
				response.setHeader("sessionstatus", "timeout");
				response.sendRedirect(request.getContextPath() + "/view/login/index");
				return false;
			}
		}

	}
}
